package com.mer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mer.entity.*;
import com.mer.mapper.*;
import com.mer.service.CourseService;
import com.mer.vo.CommonResult;
import com.mer.vo.CourseVo;
import com.mer.vo.ModifyCourseVo;
import com.mer.vo.UploadCourseVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @program: IIPhase-project
 * @description: 课程列表服务层实现类
 * @author: 陈铠
 * @create: 2021-10-20 19:48
 **/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private SubjectMapper subjectMapper;

    @Resource
    private CourseDescriptionMapper courseDescriptionMapper;

    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private CourseReviewMapper courseReviewMapper;

    /**
     * 分页查询课程列表页面
     * @param page
     * @param limit
     * @param courseVo
     * @param id
     * @return
     */
    @Override
    public CommonResult selectCourseByPage(Integer page, Integer limit, CourseVo courseVo, String id) {
        Page<CourseVo> coursePage = new Page<>();
        QueryWrapper<CourseVo> courseVoQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(courseVo.getTitle())) {
            courseVoQueryWrapper.like("title", courseVo.getTitle());
        }
        if (StringUtils.isNotEmpty(courseVo.getStatus())) {
            courseVoQueryWrapper.like("status", courseVo.getStatus());
        }
        Page<CourseVo> courseVoPage = courseMapper.selectPassedCourse(coursePage, courseVoQueryWrapper, id);
        List<CourseVo> records = courseVoPage.getRecords();
        List<Subject> subjects = subjectMapper.selectList(null);
        //拼接父课程与子课程
        for (CourseVo courseVo1 : records) {
            String ex = "";
            String nx = selectSubjectName(subjects, courseVo1.getSubjectId());
            String[] split = nx.split(">");
            for (int i = split.length - 1; i >= 0; i--) {
                if (i != 0) {
                    ex += split[i] + ">";
                } else {
                    ex += split[i];
                }
            }
            courseVo1.setSubjectName(ex);
        }
        return new CommonResult(200, "查询成功", coursePage);
    }

    /**
     * 课程分类列的递归方法，selectCourseByPage需要调用
     * @param subjects
     * @param id
     * @return
     */
    public static String selectSubjectName(List<Subject> subjects, String id) {
        String name = "";
        for (Subject subject : subjects) {
            if (id.equals(subject.getId())) {
                //拼接各个分级的课程
                name += "【" + subject.getTitle() + "】" + ">";
                if (!subject.getParentId().equals("0")) {
                    String childName = selectSubjectName(subjects, subject.getParentId());
                    name += childName;
                }
            }
        }
        return name;
    }

    /**
     * 修改课程信息方法
     * @param modifyCourseVo
     * @return
     */
    @Override
    public CommonResult modifyCourse(ModifyCourseVo modifyCourseVo) {
        int i = courseMapper.modifyCourse(modifyCourseVo);
        if (i > 0) {
            return new CommonResult(200, "修改成功", "yes");
        } else {
            return new CommonResult(500, "修改失败", "yes");
        }
    }

    @Override
    public CommonResult changeStatus(CourseVo courseVo) {
        int i = courseMapper.changeStatus(courseVo.getId(), courseVo.getStatus());
        if (i > 0) {
            return new CommonResult(200, "修改成功", "yes");
        } else {
            return new CommonResult(500, "修改失败", "yes");
        }
    }

    @Override
    public CommonResult updateCourseSubject(UploadCourseVo uploadCourseVo) {
        Course course = new Course();
        course.setTitle(uploadCourseVo.getTitle());
        course.setCover(uploadCourseVo.getCover());
        /*如果免费把价格设置为零*/
        if (uploadCourseVo.getIsFree() == 1) {
            course.setPrice(BigDecimal.ZERO);
        } else {
            course.setPrice(uploadCourseVo.getPrice());
        }

        /*类别3为空就设置父id，接着设置祖宗id*/
        if (StringUtils.isNotEmpty(uploadCourseVo.getCategoryId3())) {
            course.setSubjectId(uploadCourseVo.getCategoryId2());
            course.setSubjectParentId(uploadCourseVo.getCategoryId1());
        } else {
            course.setSubjectId(uploadCourseVo.getCategoryId3());
            course.setSubjectParentId(uploadCourseVo.getCategoryId2());
        }

        /*类别2同理*/
        if (StringUtils.isNotEmpty(uploadCourseVo.getCategoryId2())) {
            course.setSubjectId(uploadCourseVo.getCategoryId1());
            course.setSubjectParentId("0");
        }

        /*创建课程描述实体类*/
        CourseDescription courseDescription = new CourseDescription();

        //id不为空，是修改
        if (StringUtils.isNotEmpty(uploadCourseVo.getId())) {
            courseDescription.setId(uploadCourseVo.getId());
            courseDescription.setDescription(uploadCourseVo.getDescription());
            courseDescriptionMapper.updateById(courseDescription);

            course.setId(uploadCourseVo.getId());
            courseMapper.updateById(course);
            return new CommonResult(2000, "修改成功", null);
        }

        /*否则是新增
        * 获取请求头的token值*/
        QueryWrapper<Teacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.eq("mobile", uploadCourseVo.getMobile());
        //获取讲师信息
        Teacher teacher = teacherMapper.selectOne(teacherQueryWrapper);

        //System.out.println(Arrays.asList(teacher)+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");

        /*查看是否有相同的课程名*/
        QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.eq("title", uploadCourseVo.getTitle());
        System.out.println(teacher.getId()+"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        courseQueryWrapper.eq("teacher_id", teacher.getId());
        Course course1 = courseMapper.selectOne(courseQueryWrapper);

        if (course1 != null) {
            return new CommonResult(5000, "该课程已存在", null);
        }
        /*不为null则添加*/
        course.setTeacherId(teacher.getId());
        /*插入课程表中*/
        int insert = courseMapper.insert(course);
        /*查询到插入的此条课程*/
        QueryWrapper<Course> courseQueryWrapper1 = new QueryWrapper<>();
        courseQueryWrapper1.eq("title", uploadCourseVo.getTitle());
        Course course2 = courseMapper.selectOne(courseQueryWrapper1);

        /*创建课程审核实体类*/
        CourseReview courseReview = new CourseReview();
        courseReview.setCourseId(course2.getId());
        /*将该课程申请插入到课程审核表中*/
        int insert1 = courseReviewMapper.insert(courseReview);

        /*课程描述表中也插入此条数据*/
        courseDescription.setId(course2.getId());
        courseDescription.setDescription(uploadCourseVo.getDescription());
        courseDescriptionMapper.insert(courseDescription);

        if (insert > 0 && insert1 > 0) {
            return new CommonResult(2000, "新增课程成功", course2.getId());
        } else {
            return new CommonResult(5000, "新增课程失败", null);
        }
    }
}
